package me.fangx.zhihu.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import me.fangx.common.ui.activity.BaseAppCompatActivity;
import me.fangx.common.util.eventbus.EventCenter;
import me.fangx.common.util.netstatus.NetUtils;
import me.fangx.common.widget.loading.ProgressView;
import me.fangx.haorefresh.HaoRecyclerView;
import me.fangx.haorefresh.LoadMoreListener;
import me.fangx.zhihu.R;
import me.fangx.zhihu.adapter.CookListAdapter;
import me.fangx.zhihu.modle.bean.CookBean;
import me.fangx.zhihu.presenter.CookPresenter;
import me.fangx.zhihu.utils.BaseUtil;
import me.fangx.zhihu.view.CookListView;
import me.fangx.zhihu.view.EmptyView;

public class CollectActivity extends BaseAppCompatActivity implements CookListView {

    @Bind(R.id.cook_swiperefresh)
    SwipeRefreshLayout cook_swiperefresh;
    @Bind(R.id.cook_recycleview)
    HaoRecyclerView cook_recycleview;
    @Bind(R.id.empty_view)
    EmptyView mEmptyView;

    private Toolbar actionBarToolbar;
    private CookPresenter cookPresenter;
    private CookListAdapter cookListAdapter;
    private ArrayList<CookBean> listData = new ArrayList<>();
    private int page = 1;

    private BroadcastReceiver collectReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int position = intent.getIntExtra("position", 0);
//            CookBean cookBean = (CookBean) intent.getSerializableExtra("cookbean");
//            listData.get(position).setIsCollect(cookBean.getIsCollect());
            listData.remove(position);
            cookListAdapter.notifyDataSetChanged();
//            cookListAdapter.notifyItemRemoved(position);
            if(listData.size()==0){
                mEmptyView.show();
            }
        }
    };

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_collect;
    }

    @Override
    protected void initViewsAndEvents() {
        setupToolbar();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.blue.recipe.collect");
        registerReceiver(collectReceiver, intentFilter);
        cookListAdapter = new CookListAdapter(mContext, listData);
        cook_recycleview.setAdapter(cookListAdapter);
        cook_swiperefresh.setColorSchemeResources(R.color.textBlueDark, R.color.textBlueDark, R.color.textBlueDark,
                R.color.textBlueDark);

        cook_swiperefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                cookPresenter.loadCollectList(page);
            }
        });
        mEmptyView.setRetryListener(new EmptyView.RetryListener() {
            @Override
            public void retry() {
                mEmptyView.showProgress();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        cookPresenter.loadCollectList(page);
                    }
                }, 500);
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        cook_recycleview.setLayoutManager(layoutManager);

        //设置自定义加载中和到底了效果
        ProgressView progressView = new ProgressView(mContext);
        progressView.setIndicatorId(ProgressView.BallPulse);
        progressView.setIndicatorColor(0xff69b3e0);
        cook_recycleview.setFootLoadingView(progressView);

        TextView textView = new TextView(mContext);
        textView.setText(R.string.already_to_end);
        cook_recycleview.setFootEndView(textView);

        cook_recycleview.setLoadMoreListener(new LoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                cookPresenter.loadCollectList(page);
            }
        });


        cookPresenter = new CookPresenter(mContext);
        cookPresenter.attachView(this);
        //初次加载
        page = 1;
        cookPresenter.loadCollectList(page);
        showLoading(getString(R.string.loading));
    }

    private void setupToolbar() {
        final ActionBar ab = getActionBarToolbar();
        ab.setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setTitle(R.string.drawer_collect);
    }

    protected ActionBar getActionBarToolbar() {
        if (actionBarToolbar == null) {
            actionBarToolbar = (Toolbar) findViewById(R.id.toolbar);
            if (actionBarToolbar != null) {
                setSupportActionBar(actionBarToolbar);
            }
        }
        return getSupportActionBar();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.sample_actions, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {

    }

    @Override
    protected View getLoadingTargetView() {
        return cook_swiperefresh;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    public void refresh(List<CookBean> data) {
        hideLoading();
        cook_recycleview.refreshComplete();
        cook_recycleview.loadMoreComplete();
        cook_swiperefresh.setRefreshing(false);
        listData.clear();
        listData.addAll(data);
        cookListAdapter.notifyDataSetChanged();
        if (data.size() == 0) {
            mEmptyView.hideProgress();
            mEmptyView.show();
        } else {
            mEmptyView.hide();
        }
    }

    @Override
    public void loadMore(List<CookBean> data) {
        if (BaseUtil.isEmpty(data)) {
            cook_recycleview.loadMoreEnd();
        } else {
            listData.addAll(data);
            cookListAdapter.notifyDataSetChanged();
            cook_recycleview.loadMoreComplete();
        }
    }

    @Override
    public void showLoading(String msg) {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showError(String msg, View.OnClickListener onClickListener) {

    }

    @Override
    public void showEmpty(String msg, View.OnClickListener onClickListener) {

    }

    @Override
    public void showEmpty(String msg, View.OnClickListener onClickListener, int imageId) {

    }

    @Override
    public void showNetError(View.OnClickListener onClickListener) {

    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(collectReceiver);
        super.onDestroy();
    }
}
